import 'package:card_topia/design_specifications.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

///带有loading加载动画按钮
class LoadingButton extends StatefulWidget {
  const LoadingButton(
      {super.key,
      this.width,
      required this.text,
      required this.loadingText,
      required this.onTap,
        required this.enable});

  final double? width;
  final String text;
  final String loadingText;
  final Function onTap;
  final bool enable;
  @override
  State<LoadingButton> createState() => _LoadingButtonState();
}

class _LoadingButtonState extends State<LoadingButton> {
  bool _canClick = true;
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: ()async{
        if(widget.enable&&_canClick){
          _canClick = false;
          setState(() {

          });
          await widget.onTap();
          _canClick = true;
          setState(() {

          });
        }
      },
      child: Container(
        height: 48.w,
        width: widget.width??double.infinity,
        decoration: BoxDecoration(
            color: widget.enable?themeColor:disableColor,
            borderRadius: BorderRadius.circular(8.w)
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            !_canClick?const CupertinoActivityIndicator(color: Colors.white,):Container(),
            SizedBox(width: 8.w,),
            Text(!_canClick?widget.loadingText:widget.text,style: TextStyle(color: Colors.white,fontFamily: harmony,fontSize: 16.sp),)
          ],
        ),
      ),
    );
  }
}
